package cn.com.yto56.cainiao.exponent.front.service;

import org.apache.commons.lang.text.StrSubstitutor;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.frameworkset.elasticsearch.entity.MapRestResponse;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public final class CommonService {

    public static String compileString(String source, Map<String, String> parameter) {
        StrSubstitutor strSubstitutor = new StrSubstitutor(parameter, "${", "}");
        strSubstitutor.setEnableSubstitutionInVariables(true);
        return strSubstitutor.replace(source);
    }

    public static Map<String, String> getSearchTime(Date date, int step) {
        int s = step == 0 ? 1 : step;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, String> map = new HashMap<>();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, s);
        if (s > 0) {
            map.put("startTime", simpleDateFormat.format(date));
            map.put("endTime", simpleDateFormat.format(c.getTime()));
        } else {
            map.put("endTime", simpleDateFormat.format(date));
            map.put("startTime", simpleDateFormat.format(c.getTime()));
        }
        return map;
    }

    public static MapRestResponse search(ClientInterface client, String path, String entity) {
        return client.search(path, entity);
    }
}
